cmake_minimum_required(VERSION 3.20)
project(cef_c_cangjie_base_capi)

# 设置C标准为C11
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
# 设置c++ 标标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CEF_TARGET cef_c_cangjie_base_capi)
set(LIB_TARGET libcef_c_wrapper)

# 使用MT（多线程静态版本）运行时库（对于MSVC编译器）
if(MSVC)
    add_compile_options("/MT")
endif()

# 包含头文件目录
include_directories(
    ${PROJECT_SOURCE_DIR}
    ${PROJECT_SOURCE_DIR}/include
    ${PROJECT_SOURCE_DIR}/include/cef_c_wrapper
)

# 指定库文件路径
set(CEF_LIB_DIR ${CMAKE_SOURCE_DIR}/lib/cef)


# 源代码文件分组
file(GLOB SRC_MAIN_FILES ${PROJECT_SOURCE_DIR}/src/main.c) # 主函数文件
file(GLOB SRC_WRAPPER_FILES
    ${PROJECT_SOURCE_DIR}/src/cef_c_wrapper/*.c
    ${PROJECT_SOURCE_DIR}/src/cef_c_wrapper/tools/*.c
    ${PROJECT_SOURCE_DIR}/src/cef_c_wrapper/ipc/*.c
)
file(GLOB SRC_DLL_FILES
    ${PROJECT_SOURCE_DIR}/src/cef_c_wrapper/*.c
    ${PROJECT_SOURCE_DIR}/src/cef_c_wrapper/tools/*.c
    ${PROJECT_SOURCE_DIR}/src/cef_c_wrapper/ipc/*.c
)

set(SRC_FILE)
list(APPEND SRC_FILE ${SRC_MAIN_FILES})
list(APPEND SRC_FILE ${SRC_WRAPPER_FILES})

  # Standard libraries.
  set(CEF_STANDARD_LIBS
    comctl32.lib
    crypt32.lib
    delayimp.lib
    gdi32.lib
    rpcrt4.lib
    shlwapi.lib
    wintrust.lib
    ws2_32.lib
    )
    # Libraries required by cef_sandbox.lib.
    set(CEF_SANDBOX_STANDARD_LIBS
      Advapi32.lib
      dbghelp.lib
      Delayimp.lib
      ntdll.lib
      OleAut32.lib
      PowrProf.lib
      Propsys.lib
      psapi.lib
      SetupAPI.lib
      Shell32.lib
      Shcore.lib
      Userenv.lib
      version.lib
      wbemuuid.lib
      WindowsApp.lib
      winmm.lib
      )



# 添加可执行文件或者库目录
add_executable(${CEF_TARGET} ${SRC_FILE})

# 添加DLL库（库目标构建）
add_library(${LIB_TARGET} SHARED ${SRC_DLL_FILES})

# 链接cef和其他依赖库
target_link_libraries(${CEF_TARGET} 
            ${CEF_LIB_DIR}/libcef.lib
            ${CEF_STANDARD_LIBS}
            # ${CEF_LIB_DIR}/cef_sandbox.lib
            # ${CEF_SANDBOX_STANDARD_LIBS}
)

# target_compile_definitions(${LIB_TARGET} PRIVATE CEF_C_WRAPPER_EXPORT)

target_link_libraries(${LIB_TARGET}
    ${CEF_LIB_DIR}/libcef.lib
    ${CEF_STANDARD_LIBS}
#     ${CEF_LIB_DIR}/cef_sandbox.lib
#     ${CEF_SANDBOX_STANDARD_LIBS}
)


# Adds a manifest file to a Windows executable using mt.exe
macro(ADD_WINDOWS_MANIFEST manifest_dir target extension)
    # 获取目标输出的完整路径（例如 build/bin/yourapp.exe）
    set(TARGET_EXE_PATH $<TARGET_FILE:${target}>)

    # 构造主 manifest 文件路径
    set(MAIN_MANIFEST "${manifest_dir}/${target}.${extension}.manifest")

    # 可选：添加其他依赖 manifest（如 compatibility.manifest）
    set(COMPAT_MANIFEST "${manifest_dir}/compatibility.manifest")

    # 添加 post-build 自定义命令
    add_custom_command(
        TARGET ${target}
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E echo "Embedding manifest: ${MAIN_MANIFEST}"
        COMMAND mt.exe -nologo
            -manifest ${MAIN_MANIFEST}
            ${COMPAT_MANIFEST}
            -outputresource:${TARGET_EXE_PATH};#1
        COMMENT "Embedding manifest into ${target}.${extension}"
    )

    # 可选：验证 manifest 文件是否存在
    if(NOT EXISTS ${MAIN_MANIFEST})
        message(WARNING "Manifest file not found: ${MAIN_MANIFEST}")
    endif()
endmacro()


  ADD_WINDOWS_MANIFEST("${CMAKE_CURRENT_SOURCE_DIR}/win" "${CEF_TARGET}" "exe")
# ADD_WINDOWS_MANIFEST("${CMAKE_CURRENT_SOURCE_DIR}/win" "${LIB_TARGET}" "dll")

# 拷贝resoureces 文件
file(COPY ${PROJECT_SOURCE_DIR}/resources/ DESTINATION ${PROJECT_BINARY_DIR})

# 获取 lib/cef 目录下的所有文件
file(GLOB CEF_FILES ${PROJECT_SOURCE_DIR}/lib/cef/*)

# 遍历文件列表并复制它们
foreach(FILE ${CEF_FILES})
  file(COPY ${FILE} DESTINATION ${PROJECT_BINARY_DIR})
endforeach()

#
# Windows configuration.
#
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
    
endif()